package com.shouxin.attendance.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.shouxin.attendance.App;

import java.util.Set;
import java.util.TreeSet;

public class SharedPref {

    private static final String DEFAULT = "Attendance";

    private static SharedPreferences mSharedPreferences;

    static {
        mSharedPreferences = App.getApp().getSharedPreferences(DEFAULT, Context.MODE_PRIVATE);
    }

    private SharedPref() {

    }

    /**
     * 写入字符串信息
     *
     * @param key   键
     * @param value String
     */
    public static void putString(String key, String value) {
        mSharedPreferences.edit().putString(key, value).apply();
    }

    /**
     * 获取字符串信息
     *
     * @param key      键
     * @param defValue 默认值
     * @return String
     */
    public static String getString(String key, String defValue) {
        return mSharedPreferences.getString(key, defValue);
    }

    /**
     * 写入long型数据
     *
     * @param key   键
     * @param value long
     */
    public static void putLong(String key, long value) {
        mSharedPreferences.edit().putLong(key, value).apply();
    }

    /**
     * 获取long型数据
     *
     * @param key      键
     * @param defValue 默认值
     * @return long
     */
    public static long getLong(String key, long defValue) {
        return mSharedPreferences.getLong(key, defValue);
    }

    /**
     * 写入int数据
     *
     * @param key   键
     * @param value int
     */
    public static void putInt(String key, int value) {
        mSharedPreferences.edit().putInt(key, value).apply();
    }

    /**
     * 获取int数据
     *
     * @param key      键
     * @param defValue 默认值
     * @return int
     */
    public static int getInt(String key, int defValue) {
        return mSharedPreferences.getInt(key, defValue);
    }

    /**
     * 写入boolean值
     *
     * @param key  键
     * @param flag boolean
     */
    public static void putBoolean(String key, boolean flag) {
        mSharedPreferences.edit().putBoolean(key, flag).apply();
    }

    /**
     * 获取boolean值
     *
     * @param key      键
     * @param defValue 默认值
     * @return boolean
     */
    public static boolean getBoolean(String key, boolean defValue) {
        return mSharedPreferences.getBoolean(key, defValue);
    }

    /**
     * 写入float值
     *
     * @param key 键
     * @param value 默认值
     */
    public static void putFloat(String key, float value) {
        mSharedPreferences.edit().putFloat(key, value).apply();
    }

    /**
     * 获取float值
     *
     * @param key      键
     * @param defValue 默认值
     * @return float
     */
    public static float getFloat(String key, float defValue) {
        return mSharedPreferences.getFloat(key, defValue);
    }

    /**
     * 删除某项
     *
     * @param key 键
     */
    public static void remove(String key) {
        mSharedPreferences.edit().remove(key).apply();
    }

    /**
     * 是否已经存在某项
     *
     * @param key 键
     * @return 存在返回true，否则返回false
     */
    public static boolean exist(String key) {
        return mSharedPreferences.contains(key);
    }

    public static Set<String> getSetString(String key) {
        return mSharedPreferences.getStringSet(key, new TreeSet<String>());
    }

    public static void putSetString(String key, Set<String> value) {
        mSharedPreferences.edit().putStringSet(key, value).apply();
    }

}
